10 / 19

What is a Dynamic Array (ArrayList/Vector)? How does it grow?

Dynamic Arrays

javascript
  1. 1

    Capacity is different from the current logical size.

  2. 2

    When capacity is exhausted, a larger backing array is allocated.

  3. 3

    Existing elements are copied into the new array.

  4. 4

    The growth factor is implementation-dependent.

  5. 5

    The resize operation itself is O(n), but ordinary append is amortized O(1).

Difficulty: 2/10

Follow-up Questions

  • Why is append amortized O(1)?
  • What is the difference between size and capacity?